package com.xuecheng.content.api;

import com.xuecheng.base.exception.ValidationGroups;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseCategoryService;
import com.xuecheng.content.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Mr.M
 * @version 1.0
 * @description TODO
 * @date 2025/2/11 15:44
 */
@Api(value = "课程信息管理接口", tags = "课程信息管理接口")
@RestController
@Slf4j
public class CourseBaseInfoController {

    @Autowired
    private CourseBaseService courseBaseService;
    @Autowired
    private CourseCategoryService courseCategoryService;


    @ApiOperation("课程查询接口")
    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")//指定权限标识符
    @PostMapping("/course/list")
    public PageResult<CourseBase> list(PageParams pageParams, @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto) {
        log.info("这是一个课程查询接口！！！");
        //取出用户身份
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        //机构id
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())) {
            companyId = Long.parseLong(user.getCompanyId());
        }

        PageResult<CourseBase> pageResult = courseBaseService.queryCourseBaseList(companyId, pageParams, queryCourseParamsDto);
        return pageResult;

    }

    @ApiOperation("新增课程基础信息")
    @PostMapping("/course")
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated(ValidationGroups.Inster.class) AddCourseDto addCourseDto) {
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseService.createCourseBase(companyId, addCourseDto);
    }

    @ApiOperation("根据课程id查询课程基本信息")
    @GetMapping("/course/{courseId}")
    public CourseBaseInfoDto courseBaseInfoDto(@PathVariable Long courseId) {
        //获取当前用户身份
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println(principal);
        SecurityUtil.XcUser user = SecurityUtil.getUser();
        System.out.println(user.getUsername());
        log.info("课程信息id,{}", courseId);
        //查询课程信息
        CourseBase courseBase = courseBaseService.getById(courseId);
        if (courseBase == null) {
            return null;
        }
        //查询课程营销信息
        CourseCategory courseCategory = courseCategoryService.getById(courseId);
        //创建CourseBaseInfoDto对象返回
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase, courseBaseInfoDto);
        if (courseCategory != null) {
            BeanUtils.copyProperties(courseCategory, courseBaseInfoDto);
        }
        //查询分类名称
        String st = courseBaseInfoDto.getSt();
        String mt = courseBaseInfoDto.getMt();
        Map<String, String> map = courseCategoryService.lambdaQuery().in(CourseCategory::getId, Arrays.asList(st, mt))
                .list().stream().collect(Collectors.toMap(CourseCategory::getId, CourseCategory::getName));
        courseBaseInfoDto.setMtName(map.get(mt));
        courseBaseInfoDto.setStName(map.get(st));
        return courseBaseInfoDto;
    }

    @ApiOperation("修改课程基本信息")
    @PutMapping("/course")
    public CourseBaseInfoDto modifyCourseBase(@RequestBody @Validated EditCourseDto editCourseDto) {
        log.info("修改课程基本信息");
        //机构id，由于认证系统没有上线暂时硬编码
        Long companyId = 1232141425L;
        return courseBaseService.updateCourseBase(companyId, editCourseDto);
    }


    @ApiOperation("删除课程信息")
    @DeleteMapping("/course/{id}")
    public void deleteCourseBase(@PathVariable Long id) {
        log.info("课程id：{}", id);
        Long companyid = 1232141425L;
        courseBaseService.deleteCourseBase(companyid, id);
    }
}
